package Basic;
/*
 * Description:动态查询语句演示
 * Author: AMark
 * Date:11/13/2023
 * Time:8:13 AM
 */

import javax.sql.DataSource;
import java.sql.*;

public class DQ {
    static final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
    static final String URL = "jdbc:mysql://localhost:3306/hw;?serverTimezone=UTC";
    static final String USER = "root";
    static final String PASSWORD = "zhc20040309";
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //链接数据库

        Class.forName(JDBC_DRIVER);
        Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
        //执行速度更快，同时也可以防止sql注入
        //todo 这里有问题没解决,目前是sql驱动的版本问题
        String query = "SELECT * FROM students WHERE name = ? AND age = ?";
        //预处理语句，可以防止sql注入
        PreparedStatement pstm = conn.prepareStatement(query);
        pstm.setString(1, "Mark"); //这一行是给第一个问号赋值
        pstm.setInt(2, 19); //这一行是给第二个问号赋值
        ResultSet rs = pstm.executeQuery(query);
        if (rs.next())
            System.out.println("姓名：" + rs.getString("name") + "年龄：" + rs.getInt("age"));
        else
            System.out.println("没有找到相应的数据");
    }


}
